package ch.zbw.pra.sysventory.ui.gui.login;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import ch.zbw.pra.sysventory.domain.businesslogic.DomainController;
import ch.zbw.pra.sysventory.domain.factories.FileGenerator;
import ch.zbw.pra.sysventory.domain.factories.FileGenerator.FileType;
import ch.zbw.pra.sysventory.ui.gui.utilities.DefaultFonts;
import ch.zbw.pra.sysventory.ui.gui.utilities.DefaultFonts.FontType;
import ch.zbw.pra.sysventory.ui.gui.utilities.GuiLayout;
import ch.zbw.pra.sysventory.ui.gui.utilities.TextLayout;

public class LoginFrame extends JDialog implements KeyListener, MouseListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -84173286853015199L;

	private String currentUserName;
	private String currentUserPassword;
	private boolean canLogin;

	private JTextArea loginFrameWelcomeMessage;
	private JLabel userNameLoginLabel;
	private JLabel passwordLoginLabel;
	private JTextField userNameLoginTextField;
	private JPasswordField userPasswordLoginTextField;
	private JButton loginButton;

	@SuppressWarnings("unused")
	private final DefaultFonts font;

	public LoginFrame(String title) {
		super();
		this.setTitle(title + "  >> Login Dialog <<");
		this.font = new DefaultFonts();
		this.init();
	}

	public void init() {
		this.canLogin = false;
		JPanel loginPanel = new JPanel();

		try {
			this.setIconImage(new ImageIcon(new FileGenerator().getFile(FileType.ICON_PGS).getAbsolutePath()).getImage());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Bild laden fehlgeschlagen");
		}
		GridBagLayout gridBagLayout = GuiLayout.getGridBagLayout(loginPanel);
		int rowHeight = 50;
		gridBagLayout.rowHeights = new int[] { rowHeight * 2, rowHeight, rowHeight, rowHeight };
		gridBagLayout.columnWeights = new double[] { 0.0, 1.0 };
		gridBagLayout.columnWidths = new int[] { 140, 150 };
		GridBagConstraints gridBagConstraints = GuiLayout.getGridBagConstraints();

		this.loginFrameWelcomeMessage = new JTextArea("Willkommen zu SysVentory! \n  ***Bitte loggen Sie sich ein!***");
		TextLayout.addStyleToTextComponentAndNotEditable(this.loginFrameWelcomeMessage, FontType.BIG, false);
		Insets insets = new Insets(2, 10, 2, 5);
		this.loginFrameWelcomeMessage.setMargin(insets);
		int x = 0;
		int y = 0;
		int reminder = GridBagConstraints.REMAINDER;
		gridBagConstraints.fill = GridBagConstraints.BOTH;
		GuiLayout.addToLayout(loginPanel, gridBagLayout, gridBagConstraints, x, y++, 2, 1, this.loginFrameWelcomeMessage);
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		this.userNameLoginLabel = new JLabel("Username");
		TextLayout.addStyleToComponent(this.userNameLoginLabel, FontType.BIG);
		// this.userNameLoginLabel.setFont(this.font.getFontBig());
		this.userNameLoginTextField = new JTextField();
		TextLayout.addStyleToComponent(this.userNameLoginTextField, FontType.BIG);
		this.userNameLoginTextField.setMargin(insets);
		this.userNameLoginTextField.setToolTipText("Bitte Benutzername eingeben");
		this.userNameLoginTextField.addFocusListener(new FocusAdapter() {

			@Override
			public void focusGained(FocusEvent evt) {
				LoginFrame.this.userNameLoginTextField.selectAll();
			}
		});
		this.userNameLoginTextField.addKeyListener(this);
		GuiLayout.addNextRowToLayout(loginPanel, gridBagLayout, gridBagConstraints, x, y++, this.userNameLoginLabel, this.userNameLoginTextField);

		this.passwordLoginLabel = new JLabel("Passwort");
		TextLayout.addStyleToComponent(this.passwordLoginLabel, FontType.BIG);
		this.userPasswordLoginTextField = new JPasswordField();
		// this.userPasswordLoginTextField.setFont(this.font.getFontBig());
		TextLayout.addStyleToComponent(this.userPasswordLoginTextField, FontType.BIG);
		this.userPasswordLoginTextField.setMargin(insets);
		this.userPasswordLoginTextField.setToolTipText("Bitte Passwort eingeben");
		this.userPasswordLoginTextField.addFocusListener(new FocusAdapter() {

			@Override
			public void focusGained(FocusEvent evt) {
				LoginFrame.this.userPasswordLoginTextField.selectAll();
			}
		});
		this.userPasswordLoginTextField.addKeyListener(this);
		GuiLayout.addNextRowToLayout(loginPanel, gridBagLayout, gridBagConstraints, x, y++, this.passwordLoginLabel, this.userPasswordLoginTextField);

		this.loginButton = new JButton("Anmelden");
		TextLayout.addStyleToComponent(this.loginButton, FontType.BIG);
		this.loginButton.addKeyListener(this);
		this.loginButton.addMouseListener(this);
		gridBagConstraints.fill = GridBagConstraints.BOTH;
		GuiLayout.addToLayout(loginPanel, gridBagLayout, gridBagConstraints, x, y++, 2, reminder, this.loginButton);

		// GuiLayout.addToLayout(this.loginPanel, gridBagLayout,
		// gridBagConstraints, x, y++, 1, 1, new JLabel());

		this.add(loginPanel);

		// setLogonOnlyForTest();

		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.pack();
		GuiLayout.centerWindow(this);
		this.setModal(true);
		this.setVisible(true);
	}

	// public void setLogonOnlyForTest() {
	// this.userNameLoginTextField.setText("inventor");
	// this.userPasswordLoginTextField.setText("inventor");
	// }

	public void doLogin() {
		setCurrentUserName();
		setCurrentUserPassword();

		if (!getCurrentUserName().isEmpty() && !getCurrentUserPassword().isEmpty()) {
			if (!DomainController.getInstance().checkUser(getCurrentUserName(), getCurrentUserPassword())) {
				this.canLogin = false;
				GuiLayout.changeBackgroundIfAccessDenied(this.loginFrameWelcomeMessage, "Benutzername falsch", this.userNameLoginTextField);
				GuiLayout.changeBackgroundIfAccessDenied(this.loginFrameWelcomeMessage, "Logindaten stimmen nicht...", this.userPasswordLoginTextField);
			} else {
				this.canLogin = true;
				this.setVisible(false);
			}
		} else {
			if (getCurrentUserName().isEmpty()) {
				this.canLogin = false;
				GuiLayout.changeBackgroundIfAccessDenied(this.loginFrameWelcomeMessage, "Benutzername ist leer", this.userNameLoginTextField);
				GuiLayout.changeBackgroundIfAccessDenied(this.loginFrameWelcomeMessage, "Benutzername ist leer", this.userPasswordLoginTextField);
			} else if (getCurrentUserPassword().isEmpty()) {
				GuiLayout.changeBackgroundIfAccessDenied(this.loginFrameWelcomeMessage, "Passwortfeld ist leer", this.userPasswordLoginTextField);
			}
		}
	}

	public boolean canLogin() {
		return this.canLogin;
	}

	public void setCurrentUserName() {
		this.currentUserName = this.userNameLoginTextField.getText();
	}

	public void setCurrentUserPassword() {
		this.currentUserPassword = new String(this.userPasswordLoginTextField.getPassword());
	}

	public String getCurrentUserName() {
		return this.currentUserName;
	}

	public String getCurrentUserPassword() {
		return this.currentUserPassword;
	}

	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		if (e.getKeyCode() == KeyEvent.VK_ENTER) {

			if (e.getSource() == this.loginButton) {

				doLogin();
			} else if (e.getSource() == this.userNameLoginTextField) {
				doLogin();

			} else if (e.getSource() == this.userPasswordLoginTextField) {
				doLogin();

			}
		}
	}

	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseClicked(java.awt.event.MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseEntered(java.awt.event.MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseExited(java.awt.event.MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mousePressed(java.awt.event.MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseReleased(java.awt.event.MouseEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == this.loginButton) {
			doLogin();
		}
	}

}